package com.beep.erp.model.purchase;


import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.BaseFragment;
import com.beep.erp.common.bean.PurchaseListBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.SPUtils;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.adapter.HeaderAndFooterRecyclerViewAdapter;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultFooter;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.model.PurchaseViewPageFragment;
import com.beep.erp.model.addinstock.InStockActivity;
import com.beep.erp.model.stockdetail.StockDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Subscription;

import static com.beep.erp.AppContext._context;


/**
 * 采购单
 */
public class PurchaseListFragment extends BaseFragment<PurchaseListPresenter, PurchaseListModel> implements PuchaseListContract.View, RefreshView.OnFreshListener, PurchaseListAdapter.OnItemClickLitener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_my_follow)
    CoordinatorLayout activityMyFollow;

    private PurchaseListAdapter mPurchaseListAdapter;
    private ArrayList<PurchaseListBean.BizBuyingRequisitionListBean> mPurchaseList = new ArrayList<>();
    private int mPageNum = 1;
    SPUtils spUtils;
    public static boolean isRefresh = false;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_purchase_list;
    }

    @Override
    protected void initView(View view) {
        setHasOptionsMenu(true);
        spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);

        mTipsHelper = createTipsHelper(superRefresh);
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(getActivity(), true));
        superRefresh.setFooter(new DefaultFooter(getActivity(), true));

        mPurchaseListAdapter = new PurchaseListAdapter(getActivity(), mPurchaseList);
        mPurchaseListAdapter.setOnItemClickLitener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(new HeaderAndFooterRecyclerViewAdapter(mPurchaseListAdapter));

    }

    @Override
    protected void lazyLoad() {
        if ((!isPrepared || !isVisible)) {
            return;
        }
        isPrepared = false;
        mPurchaseList.clear();
        mPageNum = 1;
        mTipsHelper.showLoading(true);
        new Handler().postDelayed(() -> {
            getPurchaseListData();
        }, 0);
    }

    private void getPurchaseListData() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("pageSize", Constants.PAGE_SIZE + "");
        map.put("pageNo", mPageNum + "");
        if (PurchaseViewPageFragment.pos == 0) {
            map.put("status", 99 + "");
            mPresenter.getPurchaseData(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", 99, Constants.PAGE_SIZE, mPageNum);
        } else if (PurchaseViewPageFragment.pos == 1) {
            map.put("status", 11 + "");
            mPresenter.getPurchaseData(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", 11, Constants.PAGE_SIZE, mPageNum);
        } else if (PurchaseViewPageFragment.pos == 2) {
            map.put("status", 12 + "");
            mPresenter.getPurchaseData(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", 12, Constants.PAGE_SIZE, mPageNum);
        }
    }

    @Override
    public void onRefresh() {
        mPurchaseList.clear();
        mPageNum = 1;
        mPurchaseListAdapter.notifyDataSetChanged();
        getPurchaseListData();
    }

    @Override
    public void onLoadmore() {
        mPageNum++;
        getPurchaseListData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isRefresh) {
            isRefresh = false;
            onRefresh();
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        //采购单详情
        StockDetailActivity.start(getActivity(), mPurchaseList.get(position).getPurchaseId());
    }

    @Override
    public void onIntoClick(View view, int position) {
        //点击入库  进入添加入库单界面
        String goodStatus = mPurchaseList.get(position).getStatus();
        if (goodStatus.equals("2") || goodStatus.equals("4") || goodStatus.equals("12"))
            return;
        InStockActivity.start(getActivity(), mPurchaseList.get(position).getPurchaseId());
    }

    @Override
    public void getData() {
    }

    @Override
    public void getPurchaseData(PurchaseListBean purchaseListBean) {
        if (ToolsUtils.isEmptyList(purchaseListBean.getBizBuyingRequisitionList()) && mPageNum > 1) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mPurchaseList.addAll(purchaseListBean.getBizBuyingRequisitionList());
        mPurchaseListAdapter.notifyDataSetChanged();
        if (purchaseListBean.getBizBuyingRequisitionList().size() == 0) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        superRefresh.onFinishFreshAndLoad();
        mTipsHelper.hideLoading();
        mTipsHelper.hideError();
        mTipsHelper.hideEmptyType();
    }

    @Override
    public void showMessage(String message) {
        if (ToolsUtils.isEmpty(message)) return;
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> {
                mTipsHelper.hideError();
                onRefresh();
            });
        } else {
            ToastUtils.showShort(message);
        }
    }

}
